package com.lh.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.lh.commonutils.R;
import com.lh.entity.EduTeacher;
import com.lh.entity.vo.TeacherQuery;
import com.lh.service.IEduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author lh
 * @since 2021-06-02
 */
@Api(value = "讲师管理接口")
//@CrossOrigin
@RestController
@RequestMapping("/edu-teacher")
public class EduTeacherController {


    @Resource
    private IEduTeacherService eduTeacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("queryAll")
    public R list() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeById(@PathVariable String id) {
        boolean b = eduTeacherService.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    @ApiOperation(value = "分页讲师列表")
    @PostMapping("{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

             @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
              @RequestBody(required = false)      TeacherQuery teacherQuery) {

        Page<EduTeacher> pageParam = new Page<>(page, limit);

        eduTeacherService.pageQuery(pageParam, teacherQuery);
        List<EduTeacher> records = pageParam.getRecords();//数据list集合
        long total = pageParam.getTotal();//总记数
        Map map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);

        return R.ok().data(map);
    }


    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){

        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }

//    根据讲师id进行查询
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("getTeacher/{id}")
    public  R getTeacher(@PathVariable String id){
//        int i =1/0;
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        System.out.println(eduTeacher);
        return  R.ok().data("eduTeacher",eduTeacher);
    }

//    讲师修改功能
    @ApiOperation(value = "根据ID修改讲师")
    @PostMapping("updateTeacher")
    public  R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = eduTeacherService.updateById(eduTeacher);
        if (b){
            return  R.ok();
        }else {
            return  R.error();
        }
    }


}

